﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Microsoft.Web.Mvc;
using StructureMap;
using Wayloop.Blog.Core.Data.Entities;
using Wayloop.Blog.Core.Services;
using Wayloop.Blog.Presentation.Controllers;


namespace Wayloop.Blog.Presentation.Extensions
{
    public static class HtmlExtensions
    {
        public static string Title(this HtmlHelper htmlHelper)
        {
            return ObjectFactory.GetInstance<IConfigurationService>().Title;
        }


        public static void RenderPartialForEach<T>(this HtmlHelper htmlHelper, string partialViewName, IEnumerable<T> items)
        {
            int itemCount = items.Count();
            int itemIndex = 0;

            foreach (var item in items) {
                htmlHelper.ViewData["ItemCount"] = itemCount;
                htmlHelper.ViewData["ItemIndex"] = itemIndex++;
                htmlHelper.RenderPartial(partialViewName, item);
            }
        }


        public static string ActionLinkForShowPost(this HtmlHelper htmlHelper, Post post)
        {
            return htmlHelper.ActionLink<PostController>(c => c.Show(post.Slug), post.Title);
        }
    }
}